// stdafx.cpp : source file that includes just the standard includes
//	GlobalShare.pch will be the pre-compiled header
//	stdafx.obj will contain the pre-compiled type information

#include "stdafx.h"

#include <ComUtil.h>
#include <comdef.h>

HINSTANCE g_OldResourceHandle = NULL;
HINSTANCE g_CurModuleHandle = NULL;
INT g_nline = -1;
CString g_strFileName;

DllExport CString GetFileName(CString strPathName)
{
	return strPathName.Mid(strPathName.ReverseFind(_T('\\'))+1);
}

DllExport CString StringFromID( UINT stringID )
{
	CString strFromID;
	if( !(strFromID.LoadString(stringID)) )
	    AfxMessageBox(_T("Error Load String"));
	return strFromID;
}


DllExport CString GetResStr(UINT id)
{
	CString s;
	s.LoadString(id);
	return s;

}

DllExport CString vtos(COleVariant& v)
{
	CString ret;
	switch(v.vt)
	{
	case VT_NULL:
	case VT_EMPTY:
		ret="";
		break;
	case VT_BSTR:
		//ret=V_BSTRT(&v);
		ret=V_BSTR(&v);
		break;
	case VT_R4:
		ret.Format("%g", (double)V_R4(&v));
		break;
	case VT_R8:
		ret.Format("%g",V_R8(&v));
		break;
	case VT_I2:
		ret.Format("%d",(int)V_I2(&v));
		break;
	case VT_I4:
		ret.Format("%d",(int)V_I4(&v));
		break;
	case VT_BOOL:
		ret=(V_BOOL(&v) ? "True" : "False");
		break;
	}
//	ret.TrimLeft();ret.TrimRight();
	return ret;
}

DllExport CString vtos(_variant_t& v)
{
	CString ret;
	switch(v.vt)
	{
	case VT_NULL:
	case VT_EMPTY:
		ret="";
		break;
	case VT_BSTR:
		ret=V_BSTR(&v);
		break;
	case VT_R4:
		ret.Format("%g", (double)V_R4(&v));
		break;
	case VT_R8:
		ret.Format("%g",V_R8(&v));
		break;
	case VT_I2:
		ret.Format("%d",(int)V_I2(&v));
		break;
	case VT_I4:
		ret.Format("%d",(int)V_I4(&v));
		break;
	case VT_BOOL:
		ret=(V_BOOL(&v) ? "True" : "False");
		break;
	}
//	ret.TrimLeft();ret.TrimRight();
	return ret;
}

DllExport int vtoi(_variant_t & v)
{
	CString tmps;
	int ret=0;
	
	switch(v.vt)
	{
	case VT_NULL:
	case VT_EMPTY:
		ret = 0;
		break;
	case VT_I2:
		ret= (int)V_I2(&v);
		break;
	case VT_I4:
		ret= V_I4(&v);
		break;
	case VT_R4:
		ret= (int)V_R4(&v);
		break;
	case VT_R8:
		ret =(int)V_R8(&v);
		break;
	case VT_BSTR:
		tmps=(char*)_bstr_t(&v);
		ret=atoi((LPCSTR)tmps);
		break;
	}
	return ret;
}

DllExport int vtoi(COleVariant & v)
{
	CString tmps;
	int ret=0;
	switch(v.vt)
	{
	case VT_NULL:
	case VT_EMPTY:
		ret = 0;
		break;
	case VT_I2:
		ret= (int)V_I2(&v);
		break;
	case VT_I4:
		ret= V_I4(&v);
		break;
	case VT_R4:
		ret= (int)V_R4(&v);
		break;
	case VT_R8:
		ret =(int)V_R8(&v);
		break;
	case VT_BSTR:
		tmps=V_BSTRT(&v);
		ret=atoi((LPCSTR)tmps);
		break;
	}
	return ret;
}

DllExport float vtof(COleVariant &v)
{
	float ret=0;
	CString tmps;
	switch(v.vt)
	{
	case VT_NULL:
	case VT_EMPTY:
		ret = 0;
		break;
	case VT_R4:
		ret = V_R4(&v);
		break;
	case VT_R8:
		ret=(float)V_R8(&v);
		break;
	case VT_I2:
		ret= (float)V_I2(&v);
		break;
	case VT_I4:
		ret= (float)V_I4(&v);
		break;
	case VT_BSTR:
		tmps=V_BSTRT(&v);
		ret=static_cast<float>(atof((LPCSTR)tmps));
		break;
	}
	return ret;
}
DllExport float vtof(_variant_t &v)
{
	float ret=0;
	CString tmps;
	switch(v.vt)
	{
	case VT_NULL:
	case VT_EMPTY:
		ret = 0;
		break;
	case VT_R4:
		ret = V_R4(&v);
		break;
	case VT_R8:
		ret=(float)V_R8(&v);
		break;
	case VT_I2:
		ret= (float)V_I2(&v);
		break;
	case VT_I4:
		ret= (float)V_I4(&v);
		break;
	case VT_BSTR:
		tmps=(char*)_bstr_t(&v);
		ret=static_cast<float>(atof((LPCSTR)tmps));
		break;
	}
	return ret;
}

DllExport BOOL vtob( VARIANT &v)
{
	switch(v.vt)
	{
	case VT_BOOL:
		return (BOOL)V_BOOL(&v);
	case VT_I2:
		return (BOOL)V_I2(&v);
	case VT_I4:
		return (BOOL)V_I4(&v);
	case VT_R4:
		return (BOOL)V_R4(&v);
	case VT_R8:
		return (BOOL)V_R8(&v);
	}
	return FALSE;
}

DllExport BOOL vtob( _variant_t &v)
{
	switch(v.vt)
	{
	case VT_BOOL:
		return (BOOL)V_BOOL(&v);
	case VT_I2:
		return (BOOL)V_I2(&v);
	case VT_I4:
		return (BOOL)V_I4(&v);
	case VT_R4:
		return (BOOL)V_R4(&v);
	case VT_R8:
		return (BOOL)V_R8(&v);
	}
	return FALSE;
}

DllExport BOOL IsNumber(CString& str)
{
	TCHAR c;
	for(int i = 0; i < str.GetLength(); i++)
	{
		c = str.GetAt(i);
		if((c >= '0' && c <= '9') || c == '.' || c == '-')
		{
			if(i == 0 && c == '.')
			{
				return FALSE;
			}
			else if(str.GetLength() == 1 && c == '-')
			{
				return FALSE;
			}
			else if(str.GetLength() > 1 && i != 0 && c == '-')
			{
				return FALSE;
			}
		}
		else
		{
			return FALSE;
		}
	}
	if(str.IsEmpty())
	{
		str = _T("0");
	}
	return TRUE;
}

DllExport CString ftos(float v)
{
	CString s;
	s.Format("%g",v);
	return s;
}
DllExport CString ftos1(float v)
{
	CString s;
	long lv=(long)(v+0.5);
	s.Format("%d",lv);
	return s;
}
DllExport CString ltos(long v)
{
	CString s;
	s.Format("%d",v);
	return s;
}


DllExport void SetWindowCenter(HWND hWnd)
{
	long SW,SH;
	if(hWnd==NULL)
		return;
	SW=::GetSystemMetrics(SM_CXSCREEN);
	SH=::GetSystemMetrics(SM_CYSCREEN);
	CRect rc;
	::GetWindowRect(hWnd,&rc);
	long x=(SW-rc.Width()) / 2;
	long y=(SH-rc.Height()) / 2;
	::SetWindowPos(hWnd,NULL,x,y,0,0,SWP_NOSIZE|SWP_NOZORDER|SWP_NOACTIVATE);
}

int CALLBACK EnumFontProc(
  const ENUMLOGFONTEX *lpelfe,    // pointer to logical-font data
  const NEWTEXTMETRICEX *lpntme,  // pointer to physical-font data
  unsigned long FontType,             // type of font
  LPARAM lParam             // application-defined data
)
{
	CString strTemp;
	strTemp = lpelfe->elfLogFont.lfFaceName;
	if(strTemp.IsEmpty())
		return 1;

	CStringArray* pstrArr = (CStringArray*)(lParam);
	pstrArr->Add(strTemp);

	return 1;
}
 
DllExport CStringArray* GetAllFontName(INT nCharSet, HDC hDC)
{
	LOGFONT log;
	memset(&log, 0, sizeof(LOGFONT));

	log.lfCharSet = nCharSet;
	CStringArray* pstrArr = new CStringArray;
	if(pstrArr == NULL)
		return NULL;

	if(hDC == NULL)
		hDC = GetDC(NULL);

	EnumFontFamiliesEx(hDC, &log, (FONTENUMPROC)EnumFontProc, (long)pstrArr, 0);

	return pstrArr;
}

DllExport void AddDirSep(CString &strPathName)
{
	if(strPathName=="")
	{
		strPathName+="\\";
		return;
	}
	strPathName.TrimLeft();
	strPathName.TrimRight();
	if(strPathName.GetAt(strPathName.GetLength()-1)!='\\')
		strPathName+="\\";
}

DllExport void RemoveDirSep(CString& strPathName)
{
	strPathName.TrimLeft();
	strPathName.TrimRight();
	while(strPathName.GetAt(strPathName.GetLength()-1)=='\\')
		strPathName=strPathName.Left(strPathName.GetLength()-1);
}

